package com.service.buyer;

import com.dao.admin.AdminNoticeDao;
import com.dao.admin.AdminTypeDao;
import com.dao.buyer.IndexDao;
import com.po.Buyerinfo;
import com.po.Goodsinfo;
import com.po.Noticeinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service("indexService")
@Transactional
public class BuyerIndexServiceImpl implements BuyerIndexService{
    @Autowired
    private IndexDao indexDao;
    @Autowired
    private AdminTypeDao adminTypeDao;
    @Autowired
    private AdminNoticeDao adminNoticeDao;
    @Override
    public String before(Model model, HttpSession session, Goodsinfo goods) {
        session.setAttribute("goodsType", adminTypeDao.selectGoodsType());
        model.addAttribute("salelist", indexDao.getSaleOrder());
        model.addAttribute("focuslist", indexDao.getFocusOrder());
        model.addAttribute("noticelist", indexDao.selectNotice());
        if(goods.getG_id() == null)
            goods.setG_id(0);
        model.addAttribute("lastedlist", indexDao.getLastedGoods());
        return "buyer/index";
    }

    @Override
    public String toRegister(Model model) {
        model.addAttribute("rbuser", new Buyerinfo());
        return "buyer/register";
    }

    @Override
    public String toLogin(Model model) {
        model.addAttribute("lbuser", new Buyerinfo());
        return "buyer/login";
    }

    @Override
    public String goodsDetail(Model model, Integer id) {
        Goodsinfo goods = indexDao.selectGoodsById(id);
        model.addAttribute("goods", goods);//将数据库读取的id拿出的goods的model存到模型参数中
        return "buyer/goodsdetail";
    }

    @Override
    public String selectANotice(Model model, Integer id) {
        Noticeinfo notice = adminNoticeDao.selectANotice(id);
        model.addAttribute("notice", notice);
        return "admin/noticeDetail";
    }

    @Override
    public String search(Model model, String mykey) {
        List<Goodsinfo> list = indexDao.search(mykey);
        model.addAttribute("searchlist", list);
        return "before/searchResult";
    }

}
